# Customise this file, documentation can be found here:
# https://github.com/fastlane/fastlane/tree/master/fastlane/docs
# All available actions: https://docs.fastlane.tools/actions
# can also be listed using the `fastlane actions` command

# Change the syntax highlighting to Ruby
# All lines starting with a # are ignored when running `fastlane`

# If you want to automatically update fastlane if a new version is available:
# update_fastlane

# This is the minimum version number required.
# Update this, if you use features of a newer version
fastlane_version "2.37.0"

default_platform :android

platform :android do
  lane :clear_artifacts do
    sh "rm -f ../app/build/outputs/apk/*/*.apk"
  end

  before_all do
    # load variables from .env file in the root if it exists
    if File.exist?('../../.env')
      open('../../.env', 'r').readlines.each do |l|
        kv = l.split('=')
        ENV[kv[0]] = kv[1].chomp if kv[1]
      end
    end

    clear_artifacts
  end

  desc "Runs all the tests"
  lane :test do
    gradle(task: "test")
  end

  desc "Submit a new Beta Build"
  lane :beta do
    # Bump version code for uploading
    increment_version_code

    # Get all correct icon sizes from source
    android_appicon(
      appicon_image_file: '../AppIcon/IconAndroid.png',
      # appicon_devices: [:phone, :tablet],  Removed as of plugin 0.11.0
      appicon_icon_types: [:launcher],
      appicon_path: 'app/src/main/res/mipmap'
    )

      # cut a build
      gradle(
        task: "assemble",
        build_type: "release",
      )

    # Upload to beta
    upload_to_play_store(track: 'beta')
  end

  desc "Playground stuff"
  lane :play do
    puts number_of_commits

    # Automatic Icon magic
    android_appicon(
      appicon_image_file: '../AppIcon/IconAndroid.png',
      appicon_icon_types: [:launcher],
      appicon_path: 'app/src/main/res/mipmap'
    )
  end

  desc "Deploy a new version to the Google Play"
  lane :deploy do
    gradle(task: "assembleRelease")
    supply
  end

  # You can define as many lanes as you want

  after_all do |lane|
    # This block is called, only if the executed lane was successful

    # slack(
    #   message: "Successfully deployed new App Update."
    # )
  end

  error do |lane, exception|
    # slack(
    #   message: exception.message,
    #   success: false
    # )
  end
end


# More information about multiple platforms in fastlane: https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Platforms.md
# All available actions: https://docs.fastlane.tools/actions

# fastlane reports which actions are used. No personal data is recorded.
# Learn more at https://github.com/fastlane/fastlane#metrics
